// pages/xiaodangan/tongji/tongji.js
const app = getApp();
var njzxw = require('../../../utils/njzxw.js');
var url = require('../../../utils/url.js');
var wxCharts = require('../../../utils/wxcharts.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiArray: [[2018], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1], ['至'], [2018], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1]],
    multiIndex:[0,0,0,0,0,0,0],
    multiIndexqd: [0, 0, 0, 0, 0, 0, 0]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //加载日期选择器
    var date = new Date();
    var year = parseInt(date.getFullYear());
    var month = date.getMonth() + 1;
    var date = date.getDate();

    var dates = [];
    if (month % 2 == 0 && month != 2){
      for (var i = 1; i <= 31; i++) {
        dates.push(i);
      }
    }else if(month % 2 == 1){
      for (var i = 1; i <= 30; i++) {
        dates.push(i);
      }
    }else {
      if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
        for (var i = 1; i <= 29; i++) {
          dates.push(i);
        }
      }else{
        for (var i = 1; i <= 28; i++) {
          dates.push(i);
        }
      }
    }
    
    var years = [];
    for (var i = year; i >= year - 10;i--){
      years.push(i);
    }
    this.data.multiArray[0] = years;
    this.data.multiArray[2] = dates;
    this.data.multiArray[4] = years;
    this.data.multiArray[6] = dates;
    this.setData({
      multiArray: this.data.multiArray,
      multiIndex: [0, month-1, date-10, 0, 0, month-1, date-1],
      multiIndexqd: [0, month - 1, date-10, 0, 0, month - 1, date-1]
    });
    // this.createCart(this);
  },
  bindMultiPickerColumnChange:function(e){
    if (e.detail.column == 0 || e.detail.column == 4){
      var year = this.data.multiArray[e.detail.column][e.detail.value];
      var month = this.data.multiIndex[e.detail.column + 1]+1;
      var dates = [];
      if (month == 2){
        if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
          for (var i = 1; i <= 29; i++) {
            dates.push(i);
          }
        } else {
          for (var i = 1; i <= 28; i++) {
            dates.push(i);
          }
        }
      }else if(month % 2 == 0){
        for (var i = 1; i <= 30; i++) {
          dates.push(i);
        }
      }else{
        for (var i = 1; i <= 31; i++) {
          dates.push(i);
        }
      }
      this.data.multiArray[e.detail.column+2] = dates;
    } else if (e.detail.column == 1 || e.detail.column == 5){
      var year = this.data.multiIndex[e.detail.column - 1];
      var month = e.detail.value+1;
      var dates = [];
      if (month == 2) {
        if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
          for (var i = 1; i <= 29; i++) {
            dates.push(i);
          }
        } else {
          for (var i = 1; i <= 28; i++) {
            dates.push(i);
          }
        }
      } else if (month % 2 == 0) {
        for (var i = 1; i <= 30; i++) {
          dates.push(i);
        }
      } else {
        for (var i = 1; i <= 31; i++) {
          dates.push(i);
        }
      }
      this.data.multiArray[e.detail.column + 1] = dates;
    }
    this.data.multiIndex[e.detail.column] = e.detail.value;
    this.setData({
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    });
  },
  bindMultiPickerChange:function(e){
    this.setData({
      multiIndex:e.detail.value,
      multiIndexqd: e.detail.value
    });
    var date_min = this.data.multiArray[0][e.detail.value[0]] + '-' + njzxw.zeroize(this.data.multiArray[1][e.detail.value[1]]) + '-' + njzxw.zeroize(this.data.multiArray[2][e.detail.value[2]]);
    var date_max = this.data.multiArray[4][e.detail.value[4]] + '-' + njzxw.zeroize(this.data.multiArray[5][e.detail.value[5]]) + '-' + njzxw.zeroize(this.data.multiArray[6][e.detail.value[6]]);
    this.loadData(date_min, date_max);
  },
  loadData:function(oper_date_min,oper_date_max){
    var $this = this;
    njzxw.ajax({
      url: url.userArchivesInfo.getOperTj,
      load: true,
      data: {
        oper_date_min: oper_date_min,
        oper_date_max: oper_date_max,
      },
      fun: function (a, b) {
        if(a){
          $this.setData({
            data: b.data.data
          });
          console.log(b.data.data);
          $this.createCart($this);
          wx.hideLoading();
        }else{
          njzxw.alert({msg:b.data.msg});
        }
      }
    });
  },

  createCart:function($this){
    console.log('====',$this.data.data.titme);
    new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: $this.data.data.titme,
      series: $this.data.data.blood_sugar,
      yAxis: {
        title: '值（mmol/l）',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: wx.getSystemInfoSync().windowWidth,
      height: 250
    });
    new wxCharts({
      canvasId: 'lineCanvasxueya',
      type: 'line',
      categories: $this.data.data.titme,
      series: $this.data.data.blood_pressure,
      yAxis: {
        title: '值 (mmHg)',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: wx.getSystemInfoSync().windowWidth ,
      height: 250
    });
    new wxCharts({
      canvasId: 'lineCanvasxueyaszy',
      type: 'line',
      categories: $this.data.data.titme,
      series: $this.data.data.blood_pressure_szy,
      yAxis: {
        title: '值 (mmHg)',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: wx.getSystemInfoSync().windowWidth,
      height: 250
    });
    new wxCharts({
      animation: true, //是否有动画
      canvasId: 'pieCanvas',
      type: 'pie',
      series: $this.data.data.blood_sugartj,
      width: wx.getSystemInfoSync().windowWidth,
      height: 250,
      dataLabel: true,
    });
    new wxCharts({
      animation: true, //是否有动画
      canvasId: 'pieCanvasxueya',
      type: 'pie',
      series: $this.data.data.blood_pressuretj,
      width: wx.getSystemInfoSync().windowWidth,
      height: 250,
      dataLabel: true,
    });
    new wxCharts({
      animation: true, //是否有动画
      canvasId: 'pieCanvasxueyaszy',
      type: 'pie',
      series: $this.data.data.blood_pressure_szytj,
      width: wx.getSystemInfoSync().windowWidth,
      height: 250,
      dataLabel: true,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var date = njzxw.format(new Date(), 'yyyy-MM-dd');
    console.log(this.data);
    var date_min = this.data.multiArray[0][this.data.multiIndex[0]] + '-' + njzxw.zeroize(this.data.multiArray[1][this.data.multiIndex[1]]) + '-' + njzxw.zeroize(this.data.multiArray[2][this.data.multiIndex[2]]);
    var date_max = this.data.multiArray[4][this.data.multiIndex[4]] + '-' + njzxw.zeroize(this.data.multiArray[5][this.data.multiIndex[5]]) + '-' + njzxw.zeroize(this.data.multiArray[6][this.data.multiIndex[6]]);
    this.loadData(date_min, date_max);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})